package com.gopay.scheduler.job;

import com.gopay.cron.service.trans.ExportExcelService;
import com.gopay.scheduler.GopayBaseJob;
import com.gopay.scheduler.service.AutoGenTrade.AutoGenTradeService;
import com.gopay.scheduler.util.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;

import javax.annotation.Resource;
import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 查询成功交易记录并自动生成excel
 * 
 * @author ocean
 *
 */
public class AutoGenTradeRecordJob extends GopayBaseJob {

	/**
	 * 每次查询并生成记录为10000条
	 */
//	private static final int EXPORT_MAX_NUM = 1000000;
	// 日志
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	@Resource(name = "autoGenTradeService")
	private AutoGenTradeService autoGenTradeService;
	@Resource(name = "exportExcelService")
	private ExportExcelService exportExcelService;
	
	private @Value("#{webProperties['transDownload_beginDate']}") String start;
	private @Value("#{webProperties['transDownload_endDate']}") String end;
	private @Value("#{webProperties['transDownload_isHistory']}") String isHistory;
	private @Value("#{webProperties['transDownload_downPath']}") String downPath;
	private @Value("#{webProperties['transDownload_retentionTime']}") String retentionTime;
	@Override
	public void doJobInternal() {
		
		try {
			//是否批量执行历史数据
			if("true".equals(isHistory)){
				 Date dBegin = null;
				 Date dEnd = null;
				 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				
				 try {
					 dBegin = sdf.parse(start);
					 dEnd = sdf.parse(end);
					 logger.error(dBegin+"时间"+dEnd);
					 List<Date> lDate = DateUtils.getDates(dBegin, dEnd);
					 for (Date date : lDate)
					 {
						 exportExcelService.exportExcel(sdf.format(date),sdf.format(DateUtils.plusDays(date,1)));
					 }
				 } catch (Exception e) {
					 e.printStackTrace();
					 logger.error("********执行生成文件错误********原因为："+e);
				 }
				
			}else{
				String delFileName=DateUtils.convertDateToStringDate("yyyyMMdd", DateUtils.plusDays(new Date(), Integer.valueOf(retentionTime)));
				//每天删除历史数据文件
				delHistoryFile(downPath+delFileName+"成功交易记录.xlsx");
				String today =DateUtils.formatDate(DateUtils.plusDays(new Date(), -1), "yyyy-MM-dd");
				String tomorrow =DateUtils.formatDate(new Date(), "yyyy-MM-dd");
				logger.info("我已经开始执行自动生成记录了");
				//生成文件
				exportExcelService.exportExcel(today, tomorrow);
			}

			logger.info("AutoGenTradeRecord生成记录成功");
		} catch (Exception e) {
			logger.info("有错误了，错误信息如下:");
			e.printStackTrace();
		}

	}

	public boolean delHistoryFile(String path){
		File file = new File(path);
		boolean result= false;
		
		if(file.exists()){
			//删除历史文件
			result=file.delete();
		}
		return result;
	}
	/**
	 * export Excel
	 * 
	 * @param args
	 * @return
	 */
	
	public static void main(String[] args) {
		AutoGenTradeRecordJob agt = new AutoGenTradeRecordJob();
		agt.delHistoryFile("D:/temp/20150701.xlsx");
		try {
			System.out.println(DateUtils.convertDateToStringDate("yyyyMMdd", DateUtils.plusDays(new Date(), -10)));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
